﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QunProject.Model;

namespace QunProject.Manage.Controllers
{
    public class HomeController : BaseController
    {
        // 主页
        public ActionResult Index()
        {
            return View();
        }
        // 首页
        public ActionResult Page()
        {
            return View();
        }
        // 修改密码
        public ActionResult Password()
        {
            return View();
        }

        // 清除缓存
        public ActionResult ClearCache()
        {
            Cache.Clear("system_menu");
            Cache.Clear("file_api_url");
            Cache.Clear("classify_cache");
            return RedirectToAction("Index");
        }
        // 退出登录
        public ActionResult LoginOut()
        {
            LoginUser = null;
            return RedirectToAction("Index", "Login");
        }
        // 修改密码
        public JsonResult AjaxUpdatePassword(string oldPassword, string newPassword1, string newPassword2)
        {
            if (!Common.Verify.VerifyLength(oldPassword, 6, 18) || !Common.Verify.VerifyLength(newPassword1, 6, 18) || !Common.Verify.VerifyLength(newPassword2, 6, 18))
                return Json(Result.Fail("密码长度限制[6-18]个字符"));
            if (newPassword1 != newPassword2)
                return Json(Result.Fail("两次输入的密码不正确"));

            var _oldPawMD5 = Common.Security.MD5Encrypt32(oldPassword);
            var _newPawMD5 = Common.Security.MD5Encrypt32(newPassword1);
            if (_oldPawMD5 != LoginUser.Password)
                return Json(Result.Fail("旧密码不正确"));

            LoginUser = EF.sys_user.Attach(LoginUser);
            LoginUser.Password = _newPawMD5;
            var isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
    }
}